#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x)
        : val(x), next(NULL) {}
};

class Solution {
   public:
    ListNode* reverseList(ListNode* head) {
        ListNode* h = head;
        vector<ListNode*> v;

        while (h) {
            v.push_back(h);
            h = h->next;
        }
        reverse(v.begin(), v.end());
        for (int i = 0; i < v.size(); i++) {
            if (i == v.size() - 1) {
                v[i]->next = NULL;
            } else {
                v[i]->next = v[i + 1];
            }
        }
        return v[0];
    }
};

// int main() {
//     return 0;
// }
